package com.mask.mk.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mask.mk.mapper.GoodsMapper;
import com.mask.mk.pojo.Cart;
import com.mask.mk.mapper.CartMapper;
import com.mask.mk.pojo.Goods;
import com.mask.mk.service.CartService;
import com.mask.mk.util.JsonResult;
import com.mask.mk.vo.cart.AddCartRequest;
import com.mask.mk.vo.cart.ListReponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author joey
 * @since 2023-04-25
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult addCart(AddCartRequest addCartRequest) {
        JsonResult result=null;
        Cart cart1 = cartMapper.selectOne(Wrappers.<Cart>lambdaQuery()
                .and(wrapper -> wrapper.eq(Cart::getUserid, addCartRequest.getUserId())
                        .eq(Cart::getGoodsid,addCartRequest.getGoodsId())));
        if(cart1 == null){
            Cart cart = new Cart();
            cart.setDel(0).setGoodsid(addCartRequest.getGoodsId())
                    .setUserid(addCartRequest.getUserId()).setGoodsnum(addCartRequest.getGoodsNum());
            int insert = cartMapper.insert(cart);
            if(insert>0){
                result = JsonResult.success("第一次添加购物车");
            }else{
                result = JsonResult.fail("添加失败");
            }
        }else{
            cart1.setGoodsnum(cart1.getGoodsnum()+1);
            cartMapper.updateById(cart1);
            result = JsonResult.success("添加成功");
        }
        return result;
    }

    @Override
    public JsonResult getCartList(int userId) {
        //根据用户id查购物车
        List<Cart> cartList = cartMapper.selectList(Wrappers.<Cart>lambdaQuery()
                .and(wrapper -> wrapper.eq(Cart::getUserid, userId)));
        JsonResult result=null;
        List<ListReponse> lrList = new ArrayList<>(); //输出列表

        //根据获取的用户id 商品id 显示购物车信息
        if(cartList!=null){
            ListReponse listReponse = null;
            for (Cart cart : cartList) {
                listReponse = new ListReponse();
                Goods goods = goodsMapper.selectById(cart.getGoodsid());
                listReponse.setCartId(cart.getCartid())
                        .setGoodsId(goods.getGid())
                        .setGname(goods.getGname())
                        .setDetailName(goods.getDetailName())
                        .setGoodsColor("黑色")
                        .setGoodsNum(cart.getGoodsnum())
                        .setImgLink(goods.getImgLink())
                        .setGprice(goods.getGprice());
                lrList.add(listReponse);
            }
            if(lrList!=null){
                result = JsonResult.success(lrList);
            }else{
                result = JsonResult.fail("购物车为空");
            }

        }else{
            result = JsonResult.fail("购物车为空");
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult delCart(Integer cartId) {
        int i = cartMapper.deleteById(cartId);
        if(i>0){
            return JsonResult.success("删除成功");
        }else{
            return JsonResult.fail("删除失败");
        }
    }


}
